package com.softeem.score.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softeem.score.common.exception.MyException;
import com.softeem.score.manager.mapper.GradeMapper;
import com.softeem.score.manager.mapper.StudentMapper;
import com.softeem.score.manager.mapper.TeachingClassMapper;
import com.softeem.score.manager.service.TeachingClassService;
import com.softeem.score.model.dto.TeachingClassDto;
import com.softeem.score.model.enmu.CourseBelongingEnum;
import com.softeem.score.model.entity.educational.Grade;
import com.softeem.score.model.entity.educational.TeachingClass;
import com.softeem.score.model.entity.school.Student;
import com.softeem.score.model.vo.common.ResultCodeEnum;
import com.softeem.score.model.vo.educational.ElectiveCourseVo;
import com.softeem.score.model.vo.educational.TeachingClassVo;
import com.softeem.score.model.vo.school.MapVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TeachingClassServiceImpl implements TeachingClassService {
    @Autowired
    private TeachingClassMapper teachingClassMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private GradeMapper gradeMapper;

    @Override
    public PageInfo<TeachingClassVo> findByPage(TeachingClassDto teachingClassDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TeachingClassVo> teachingClassVoList = teachingClassMapper.findByPage(teachingClassDto);
        for (TeachingClassVo teachingClassVo : teachingClassVoList) {
            teachingClassVo.setStudentNum(teachingClassMapper.findStudentNumByTeachingClassId(teachingClassVo.getId()));
        }
        PageInfo<TeachingClassVo> pageInfo = new PageInfo<>(teachingClassVoList);
        return pageInfo;
    }

    @Transactional
    @Override
    public void deleteById(Long teachingClassId) {
        teachingClassMapper.deleteStudentByTeachingClassId(teachingClassId);
        List<Long> allGradeIds = gradeMapper.findAllGradeIds(teachingClassId);
        gradeMapper.deleteAllByGradeIds(allGradeIds);
        gradeMapper.deleteGradeDetail(allGradeIds);
        teachingClassMapper.deleteById(teachingClassId);
    }

    @Transactional
    @Override
    public void saveTeachingClass(TeachingClass teachingClass) {
        teachingClassMapper.saveTeachingClass(teachingClass);
    }

    @Transactional
    @Override
    public void updateTeachingClass(TeachingClass teachingClass) {
        teachingClassMapper.updateTeachingClass(teachingClass);
    }

    @Transactional
    @Override
    public void saveStudentToTeachingClassByClassesId(Long teachingClassId, Long classesId) {
        List<Long> studentIds = studentMapper.findStudentIdsByClassesId(classesId);
        TeachingClass teachingClass = teachingClassMapper.findByTeachingClassId(teachingClassId);
        if (studentIds.size() > 0) {
            //判断绑定班级没有
            if (teachingClass.getClassesId() != null) {
                throw new MyException(ResultCodeEnum.TEACHINGCLASS_HAS_STUDENT_NOT_UPDATE);
            } else {
                teachingClassMapper.updateTeachingClassClassesId(classesId, teachingClassId);
                List<Long> gradeComponentCourseIds = gradeMapper.findGradeComponentCourseId(teachingClass.getCourseId());
                System.out.println("gradeComponentCourseId = " + gradeComponentCourseIds);
                for (Long studentId : studentIds) {
                    Grade grade = new Grade();
                    grade.setStudentId(studentId);
                    grade.setTeachingClassId(teachingClassId);
                    gradeMapper.saveStudentToGradeByClassesId(grade);
                    System.out.println("gradeId = " + grade);
                    gradeMapper.saveStudentToGradeDetail(grade.getId(), gradeComponentCourseIds);
                }
            }
        } else {
            throw new MyException(ResultCodeEnum.CLASS_NOT_EXIST);
        }
    }

    @Override
    public List<MapVo> findAllClassByTeachingClass(Long courseId, Integer academicYear, Integer semester) {
        return teachingClassMapper.findAllClassByTeachingClass(courseId, academicYear, semester);
    }

    @Override
    public PageInfo<ElectiveCourseVo> findByPageByOwnership(Integer ownership, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ElectiveCourseVo> electiveCourseVoList = teachingClassMapper.findByPageByOwnership(ownership);
        for (ElectiveCourseVo electiveCourseVo : electiveCourseVoList) {
            electiveCourseVo.setStudentNum(teachingClassMapper.findStudentNumByTeachingClassId(electiveCourseVo.getTeachingClassId()));
            electiveCourseVo.setOwnershipName(CourseBelongingEnum.getById(electiveCourseVo.getOwnership()).getValue());
        }
        return new PageInfo<>(electiveCourseVoList);
    }

    @Transactional
    @Override
    public void studentsCourseSelection(Long teachingClassId, String studentNumber) {
        TeachingClass teachingClass = teachingClassMapper.getCourseIdByTeachingClass(teachingClassId);
        List<Long> teachingClassIds = teachingClassMapper.FindAllByCourseId(teachingClass.getCourseId());
        //查询学生是否选择了这门课
        int count = gradeMapper.findSizeByTeachingClassId(teachingClassIds, studentNumber);
        int total = teachingClassMapper.findStudentNumByTeachingClassId(teachingClassId);
        Student student = studentMapper.findByStudentNumber(studentNumber);
        List<Long> gradeComponentCourseIds = gradeMapper.findGradeComponentCourseId(teachingClass.getCourseId());
        if (total < teachingClass.getMaxCount()) {
            if (count == 0 ){
                Grade grade = new Grade();
                grade.setStudentId(student.getId());
                grade.setTeachingClassId(teachingClassId);
                gradeMapper.saveStudentToGradeByClassesId(grade);
                gradeMapper.saveStudentToGradeDetail(grade.getId(), gradeComponentCourseIds);
            }else {
                throw new MyException(ResultCodeEnum.STUDENTHASSELECTED);
            }
        } else {
            throw new MyException(ResultCodeEnum.THEENROLMENT_IS_FULL);
        }
    }

    @Transactional
    @Override
    public void gradeDelete(Long teachingClassId, String studentNumber) {
        List<Long> gradeIds = gradeMapper.findGradeIdByStudentNumberAndTeachingClassId(studentNumber, teachingClassId);
        if (gradeIds.size() == 0) {
            throw new MyException(ResultCodeEnum.STUDENT_NOT_SELECTED);
        } else {
            gradeMapper.deleteAllByGradeIds(gradeIds);
            gradeMapper.deleteGradeDetail(gradeIds);
        }
    }

    @Override
    public List<ElectiveCourseVo> getElectiveCourse(String studentNumber) {
        return teachingClassMapper.getElectiveCourse(studentNumber);
    }
}
